package com.lovepick.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.lovepick.MsgActivity;
import com.lovepick.R;
import com.lovepick.bean.OrderInfo;
import com.lovepick.fragment.MsgFragment;
import com.lovepick.util.BaseUtils;
import com.lovepick.util.OrderState;

public class OrderInfoAdapter extends BaseAdapter
{
	private static final int ORDER_INFO_TYPE_COUNT = 2;
	private static final int ORDER_INFO_NORMAL = 0;
	private static final int ORDER_INFO_FINISHED = 1;
	private List<OrderInfo> mOrders = null;
	private Context mContext;
	private LayoutInflater mInflater;
	private boolean mIsDriver = true;

	public OrderInfoAdapter(Context context, List<OrderInfo> orders, boolean isDriver)
	{
		this.mContext = context;
		this.mOrders = orders;
		this.mIsDriver = isDriver;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount()
	{
		// TODO Auto-generated method stub
		return mOrders.size();
	}

	@Override
	public Object getItem(int position)
	{
		// TODO Auto-generated method stub
		return mOrders.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public int getViewTypeCount()
	{
		return ORDER_INFO_TYPE_COUNT;
	}

	@Override
	public int getItemViewType(int position)
	{
		OrderInfo order = mOrders.get(position);
		if (OrderState.FINISHED.equals(order.getState()))
		{
			return ORDER_INFO_FINISHED;
		}
		else
		{
			return ORDER_INFO_NORMAL;
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		OrderInfo order = mOrders.get(position);
		if (null == convertView)
		{
			if (mIsDriver)
			{
				convertView = mInflater.inflate(R.layout.order_info_item, null);
			}
			else
			{
				convertView = mInflater.inflate(R.layout.order_info_item_nodriver, null);
			}

			viewHolder = new ViewHolder();
			viewHolder.passenger = (TextView) convertView.findViewById(R.id.order_info_item_passenger);
			viewHolder.driver = (TextView) convertView.findViewById(R.id.order_info_item_driver);
			viewHolder.count = (TextView) convertView.findViewById(R.id.order_info_item_count);
			viewHolder.money = (TextView) convertView.findViewById(R.id.order_info_item_money);
			viewHolder.state = (ImageView) convertView.findViewById(R.id.order_info_item_state);
			viewHolder.call = (ImageButton) convertView.findViewById(R.id.order_info_item_call);
			viewHolder.msg = (ImageButton) convertView.findViewById(R.id.order_info_item_msg);

			convertView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) convertView.getTag();
		}

		if (order.getState().equals(OrderState.FINISHED))
		{
			viewHolder.state.setImageResource(R.drawable.ic_state_finished);
		}
		else
		{
			viewHolder.state.setImageDrawable(null);
		}

		viewHolder.passenger.setText(order.getPassengerName());
		viewHolder.count.setText(String.valueOf(order.getCount()));
		viewHolder.driver.setText(order.getDriverName());
		viewHolder.money.setText(String.valueOf(order.getMoney()));
		viewHolder.call.setOnClickListener(new ImgBtnClickListener(position));
		viewHolder.msg.setOnClickListener(new ImgBtnClickListener(position));

		return convertView;
	}

	static class ViewHolder
	{
		public TextView passenger, driver, count, money;
		public ImageView state;
		public ImageButton call, msg;
	}

	class ImgBtnClickListener implements View.OnClickListener
	{
		private int mPosition;

		public ImgBtnClickListener(int position)
		{
			mPosition = position;
		}

		@Override
		public void onClick(View v)
		{
			// TODO Auto-generated method stub
			OrderInfo order = mOrders.get(mPosition);
			final String phone = mIsDriver ? order.getDrPhone() : order.getPaPhone();
			final int id = mIsDriver ? order.getDrID() : order.getPaID();
			final String nickName = mIsDriver ? order.getDriverName() : order.getPassengerName();

			switch (v.getId())
			{
				case R.id.order_info_item_call:
					BaseUtils.callPhone(mContext, phone);
					break;

				case R.id.order_info_item_msg:
					Bundle data = new Bundle();
					data.putInt(MsgFragment.ARG_USERID, id);
					data.putString(MsgFragment.ARG_NICKNAME, nickName);
					Intent intent = new Intent(mContext, MsgActivity.class);
					intent.putExtra("bundle_msg", data);
					mContext.startActivity(intent);
					// BaseUtils.showToast(mContext, "user_id=" + id);
					break;

				default:
					break;
			}

		}

	}

}
